//
//  YSOrderDeliveryCtrl.m
//  Yueshi
//
//  Created by Northwan on 15/8/5.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//  物流查询

#import "YSOrderDeliveryCtrl.h"
#import "YSOrderDeliveryCell.h"
#import "YSOrderDeliveryHeaderView.h"
#import "YSMineDataTool.h"

@interface YSOrderDeliveryCtrl ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *itemArray;
@property (strong, nonatomic) YSOrderDeliveryHeaderView *headerView;
@property (copy, nonatomic) NSString *orderCode;
@end

@implementation YSOrderDeliveryCtrl

-(instancetype)initWithOrderCode:(NSString *)orderCode{
    self = [super init];
    if(self){
        self.orderCode = orderCode;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"物流查询"];
    _itemArray = [NSMutableArray array];
    self.headerView = [YSOrderDeliveryHeaderView headerView];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self requestData];
}

-(void)requestData{
    [YSMineDataTool getDeliveryWithOrderCode:self.orderCode success:^(YSOrderDeliveryRes *result) {
        if(result.isSucc){
                [self.tableView setTableHeaderView:self.headerView];
                [self.headerView setDelivery:result.logistics];
            if(result.logistics.data.count){
                [self.itemArray removeAllObjects];
                [self.itemArray addObjectsFromArray:result.logistics.data];
                [self.tableView reloadData];
            }
        }else{
            ShowEnterTips(result.data);
        }
    } failure:^(NSError *error) {
        ShowTips(kRequestFailure);
    }];
}

#pragma - mark UITableViewDataSource,UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YSOrderDeliveryCell *cell =  [YSOrderDeliveryCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setItem:self.itemArray[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return YSOrderDeliveryCellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
